package cn.zj.logistics.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.zj.logistics.pojo.BaseData;
import cn.zj.logistics.pojo.BaseDataExample;
import cn.zj.logistics.pojo.BaseDataExample.Criteria;
import cn.zj.logistics.pojo.MessageObject;
import cn.zj.logistics.service.BaseDataService;

@Controller
@RequestMapping("/baseData")
public class BaseDataController {

	@Autowired
	private BaseDataService baseDataService;

	@RequiresPermissions("baseData:baseDataPage")
	@RequestMapping("/baseDataPage")
	public String baseDataPage() {

		return "baseDataPage";
	}
	
	@RequiresPermissions("baseData:list")
	@RequestMapping("/list")
	@ResponseBody
	public <T> PageInfo<BaseData> list(@RequestParam(defaultValue = "1") int pageNum,
			@RequestParam(defaultValue = "10") int pageSize, String keyword) {

		PageHelper.startPage(pageNum, pageSize);

		BaseDataExample example = new BaseDataExample();

		// 前台传递关键字参数不为空的时候才参与条件查询
		if (StringUtils.isNotBlank(keyword)) {
			Criteria criteria1 = example.createCriteria();
			criteria1.andBaseNameLike("%" + keyword + "%");
			
			
			Criteria criteria2 = example.createCriteria();
			criteria2.andParentNameLike("%" + keyword + "%");
			
			//OR
			example.or(criteria2);
			
		}

		List<BaseData> baseDatas = baseDataService.selectByExample(example);

		PageInfo<BaseData> pageInfo = new PageInfo<>(baseDatas);

		return pageInfo;
	}

	@RequestMapping("/delete")
	@ResponseBody
	public MessageObject delete(Long baseId) {
		/*
		 * 从数据表中查询判断是否有parent_id 是当前要删除的id
		 * 
		 * 有：说明当前数据是一个类别数据，还有子数据，不能删除
		 * 
		 * 没有：删除
		 * 
		 */
		
		BaseDataExample example = new BaseDataExample();
		Criteria criteria = example.createCriteria();
		criteria.andParentIdEqualTo(baseId);
		List<BaseData> children = baseDataService.selectByExample(example);
		
		if(children.size() > 0) {
			return MessageObject.createMo(0, "当前类别数据还有子数据，不能删除");
		}
		

		int row = baseDataService.deleteByPrimaryKey(baseId);

		MessageObject mo = MessageObject.createMo(0, "删除数据失败，请联系技术人员");

		if (row == 1) {
			mo = MessageObject.createMo(1, "删除数据成功");
		}
		return mo;
	}

	// 新增功能
	@RequestMapping("/insert")
	@ResponseBody
	public MessageObject insert(BaseData baseData) {

		int row = baseDataService.insert(baseData);

		MessageObject mo = MessageObject.createMo(0, "新增数据数据失败，请联系技术人员");

		if (row == 1) {
			mo = MessageObject.createMo(1, "新增数据成功");
		}
		return mo;
	}

	// 新增功能
	@RequestMapping("/update")
	@ResponseBody
	public MessageObject update(BaseData baseData) {

		int row = baseDataService.updateByPrimaryKeySelective(baseData);

		MessageObject mo = MessageObject.createMo(0, "修改数据数据失败，请联系技术人员");

		if (row == 1) {
			mo = MessageObject.createMo(1, "修改数据成功");
		}
		return mo;
	}

	@RequestMapping("/baseDataEdit")
	public String baseDataEdit(Model m, Long baseId) {
		
		/*
		 * 查询出parent_id == null 父id为空的数据，就属于分类数据
		 */
		
		BaseDataExample example = new BaseDataExample();
		Criteria criteria = example.createCriteria();
		criteria.andParentIdIsNull();
		List<BaseData> parents = baseDataService.selectByExample(example );
		m.addAttribute("parents", parents);

		if (baseId != null) {
			// 查询出当前要修改的用户信息共享给编辑页面以供修改数据回显
			BaseData baseData = baseDataService.selectByPrimaryKey(baseId);
			m.addAttribute("baseData", baseData);
		}
		

		return "baseDataEdit";
	}



}
